package com.ruoyi.reportInfo.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.domain.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.ruoyi.reportInfo.domain.bo.IndustrialLogisticsCompanyLineOperationBo;
import com.ruoyi.reportInfo.domain.vo.IndustrialLogisticsCompanyLineOperationVo;
import com.ruoyi.reportInfo.domain.IndustrialLogisticsCompanyLineOperation;
import com.ruoyi.reportInfo.mapper.IndustrialLogisticsCompanyLineOperationMapper;
import com.ruoyi.reportInfo.service.IIndustrialLogisticsCompanyLineOperationService;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 物流企业专线运营信息Service业务层处理
 *
 * @author yangzhen
 * @date 2025-11-24
 */
@RequiredArgsConstructor
@Service
public class IndustrialLogisticsCompanyLineOperationServiceImpl implements IIndustrialLogisticsCompanyLineOperationService {

    private final IndustrialLogisticsCompanyLineOperationMapper baseMapper;

    /**
     * 查询物流企业专线运营信息
     */
    @Override
    public IndustrialLogisticsCompanyLineOperationVo queryById(String id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询物流企业专线运营信息列表
     */
    @Override
    public TableDataInfo<IndustrialLogisticsCompanyLineOperationVo> queryPageList(IndustrialLogisticsCompanyLineOperationBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<IndustrialLogisticsCompanyLineOperation> lqw = buildQueryWrapper(bo);
        Page<IndustrialLogisticsCompanyLineOperationVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询物流企业专线运营信息列表
     */
    @Override
    public List<IndustrialLogisticsCompanyLineOperationVo> queryList(IndustrialLogisticsCompanyLineOperationBo bo) {
        LambdaQueryWrapper<IndustrialLogisticsCompanyLineOperation> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<IndustrialLogisticsCompanyLineOperation> buildQueryWrapper(IndustrialLogisticsCompanyLineOperationBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<IndustrialLogisticsCompanyLineOperation> lqw = Wrappers.lambdaQuery();
        lqw.eq(StringUtils.isNotBlank(bo.getCompanyId()), IndustrialLogisticsCompanyLineOperation::getCompanyId, bo.getCompanyId());
        lqw.eq(StringUtils.isNotBlank(bo.getStartPoint()), IndustrialLogisticsCompanyLineOperation::getStartPoint, bo.getStartPoint());
        lqw.eq(StringUtils.isNotBlank(bo.getEndPoint()), IndustrialLogisticsCompanyLineOperation::getEndPoint, bo.getEndPoint());
        lqw.eq(bo.getReportDate() != null, IndustrialLogisticsCompanyLineOperation::getReportDate, bo.getReportDate());
        lqw.eq(bo.getFreightVolume() != null, IndustrialLogisticsCompanyLineOperation::getFreightVolume, bo.getFreightVolume());
        lqw.eq(bo.getReportCollectDate() != null, IndustrialLogisticsCompanyLineOperation::getReportCollectDate, bo.getReportCollectDate());
        return lqw;
    }

    /**
     * 新增物流企业专线运营信息
     */
    @Override
    public Boolean insertByBo(IndustrialLogisticsCompanyLineOperationBo bo) {
        IndustrialLogisticsCompanyLineOperation add = BeanUtil.toBean(bo, IndustrialLogisticsCompanyLineOperation.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改物流企业专线运营信息
     */
    @Override
    public Boolean updateByBo(IndustrialLogisticsCompanyLineOperationBo bo) {
        IndustrialLogisticsCompanyLineOperation update = BeanUtil.toBean(bo, IndustrialLogisticsCompanyLineOperation.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(IndustrialLogisticsCompanyLineOperation entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除物流企业专线运营信息
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<String> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
